home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 28 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: in2.uu.net!bounce-back
  2. Date: 10 Jan 96 23:17:42 GMT
  3. Approved: fjh@cs.mu.oz.au
  4. From: clamage@Eng.Sun.COM (Steve Clamage)
  5. Newsgroups: comp.std.c++
  6. Subject: Re: Q: f( const T )
  7. X-Original-Date: 10 Jan 1996 16:22:57 GMT
  8. Organization: Sun Microsystems Inc.
  9. Message-ID: <4d0p51$il8@engnews1.Eng.Sun.COM>
  10. References: <4cvsps$5ig@dub-news-svc-4.compuserve.com>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMPRJL+EDnX0m9pzZAQHpIQGAn9dI8Fq+PqOHJlBzuKJdd4R3qDxjuxaU
  14.     txrVZ3VeMBdr+lMg73I51/xgz20tmMWT
  15.     =gC1f
  16.  
  17. In article 5ig@dub-news-svc-4.compuserve.com,
  18. 100754.2730@compuserve.com (Martin Aupperle) writes:
  19. >If I have a function declared as
  20. >
  21. >  f( const T );
  22. >
  23. >will a local T be created and initialized with the copy constructor? 
  24. >I hope so, but people pointed out to me that f cannot change anything
  25. >in the object and therefore can work on the caller's object too, and
  26. >therefore a compiler may optimize away the local object.
  27. >What about mutable members? Since we now can have these, it makes a
  28. >difference. Who is right?
  29.  
  30. The semantics implemented by the compiler must be those of pass by value,
  31. whatever mechanism is actually used. In particular, if the program
  32. contains only code with defined behavior, the actual argument must
  33. not be changed by the call to f() -- whether the formal argument is
  34. declared const or not.
  35.  
  36. In particular, if type T has a mutable member, the compiler cannot pass
  37. the actual argument by reference unless it can prove that the argument
  38. is in fact never altered.
  39.  
  40. If the formal argument is declared const and has no mutable members,
  41. casting away const and attempting to modify the parameter has undefined
  42. results.
  43.  
  44. The fundamental rule is the "as-if" rule. The language definition
  45. describes the effects of various constructs. The standard describes an
  46. abstract machine for purposes of explanation of the required effects.
  47. An implementation that behaves (so far as a valid program can detect)
  48. as if it were the abstract machine is a valid implementation.
  49. ---
  50. Steve Clamage, stephen.clamage@eng.sun.com
  51. ---
  52. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  53.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  54.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  55.